package com.qingyun.customview.draw.demo

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

class MyTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = android.R.attr.textViewStyle,
) : AppCompatTextView(context, attrs, defStyleAttr) {

    private val mPaintBlack = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL
    }
    private val mPaintRed = Paint().apply {
        color = Color.RED
        style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas) {
        val w = measuredWidth.toFloat()
        val h = measuredHeight.toFloat()
        canvas.drawRect(0f, 0f, w, h, mPaintBlack)
        canvas.drawRect(10f, 10f, w - 10f, h - 10f, mPaintRed)
        canvas.save()
        canvas.translate(10f, 10f)
        super.onDraw(canvas)
        canvas.restore()
    }
}